package com.chengzhi.service.Impl;

import com.chengzhi.service.TccService;
import io.seata.core.context.RootContext;
import io.seata.rm.tcc.api.BusinessActionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.Map;


/**
 * @description:
 * @author: ruanchengzhi
 * @create: 2020-09-01 19:48
 **/
@Service
public class TccServiceImpl implements TccService {
    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Override
    public String prepare(Map<String, String> params) {
        log.info("prepare ------------------> xid = " + RootContext.getXID());
        String code = params.get("code");
        if (code.equals("2")) {
            throw new RuntimeException("rollback()");
        }
        return "success";
    }

    @Override
    public boolean commitTcc(BusinessActionContext context) {
        log.info("commitTcc ------------------> xid = " + context.getXid());
        return true;
    }

    @Override
    public boolean cancel(BusinessActionContext context) {
        log.info("cancel ------------------> xid = " + context.getXid());
        return true;
    }
}
